package com.keeson.android_cli.base.retrofit;


import com.keeson.android_cli.R;
import com.keeson.android_cli.exception.NetConnException;
import com.keeson.android_cli.util.NetUtils;
import com.keeson.android_cli.util.ResUtils;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 线程切换
 * Created by gh on 2018/7/27.
 */
public class RxSchedulers {

    public static ObservableTransformer io_main() {

        return new ObservableTransformer() {
            @Override
            public ObservableSource apply(Observable observable) {
                return observable
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .doOnSubscribe(new Consumer<Disposable>() {
                            @Override
                            public void accept(Disposable disposable) throws Exception {

                                if (!NetUtils.isCon()) {
                                    if (!disposable.isDisposed()) {
                                        disposable.dispose();
                                    }
                                    throw new NetConnException(ResUtils.getText(R.string.str_net_error));
                                }

                            }
                        });
            }
        };

    }
}
